package com.yhcnet.blog.service;

import com.yhcnet.blog.dao.EssayMapper;
import com.yhcnet.blog.dao.UserMapper;
import com.yhcnet.blog.pojo.Essay;
import com.yhcnet.blog.pojo.User;
import com.yhcnet.blog.reponse.EssayBody;
import com.yhcnet.blog.reponse.EssayEditBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EssayService {
    @Autowired
    EssayMapper essayMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    /**
     * 同步锁，进行插入文章，防止主键重复
     * @param essayBody
     * @return
     */
    public synchronized boolean create(EssayBody essayBody){
        //判断用户是否登录
        String UUID = essayBody.getUuid();
        if(!userService.judge(UUID,essayBody.getEmail()))return false;
        //登录了，创建文章
        Essay essay = new Essay(-1,essayBody.getTitle(),essayBody.getContent(),null,essayBody.getEmail(),0,0,essayBody.getKind());
        int i = essayMapper.create(essay);
        if(i > 0) return true;
        return false;
    }

    /**
     * 删除文章
     * @param eid
     * @param uuid
     * @param email
     * @return
     */
    public boolean delete(int eid,String uuid,String email){
        if(!userService.judge(uuid,email)) return false;
        int i = essayMapper.deleteE(eid);
        if(i > 0) return true;
        return false;
    }

    /**
     * 获取某一篇文章
     * @param EID
     * @return
     */
    public Essay getByEID(int EID){
        Essay byEID = essayMapper.getByEID(EID);
        if(byEID != null){
            essayMapper.visited(EID);
            return byEID;
        }
        return null;
    }

    /**
     * 获取总数
     * @return
     */
    public int count(){
        return essayMapper.count();
    }

    /**
     * 分页功能
     * @param page
     * @return
     */
    public List<Essay> getListByLimit(int page){
        return essayMapper.getListByLimit((page-1)*9);
    }

    public List<Essay> getListBylist(int page){
        return essayMapper.getListBylist((page-1)*5);
    }

    /**
     * 获取最热的八篇文章
     * @return
     */
    public List<Essay> getHotEssay(){
        return essayMapper.getHotEssay();
    }

    /**
     * 编辑博文
     * @param essayEditBody
     * @return
     */
    public synchronized boolean edit(EssayEditBody essayEditBody){
        if(essayEditBody == null) return false;
        boolean judge = userService.judge(essayEditBody.getUuid(), essayEditBody.getEmail());
        if(!judge) return false;
        Essay essay = new Essay(essayEditBody.getEid(),essayEditBody.getTitle(),essayEditBody.getContent(),null,essayEditBody.getEmail(),0,0,essayEditBody.getKind());
        int edit = essayMapper.edit(essay);
        if(edit > 0) return true;
        return false;
    }

    public List<Essay> kind(String kind){
        return essayMapper.kind(kind);
    }

    public int visitedall(){
        return essayMapper.visitedall();
    }

    public List<Essay> getListByEmail(String email){
        return essayMapper.getListByEmail(email);
    }
}
